package com.example.service;

import com.example.entity.SysEmployee;
import com.example.exception.CustomException;
import com.example.mapper.SysEmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysEmployeeService {

    @Resource
    private SysEmployeeMapper sysEmployeeMapper;
    public List<SysEmployee> selectAll(SysEmployee sysEmployee) {
        return sysEmployeeMapper.selectAll(sysEmployee);
    }

    //分页查询
    public PageInfo<SysEmployee> selectPage(SysEmployee sysEmployee, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysEmployee> list = sysEmployeeMapper.selectAll(sysEmployee);
        return PageInfo.of(list);
    }

    public void add(SysEmployee sysEmployee) {
        sysEmployeeMapper.insert(sysEmployee);
    }

    public void update(SysEmployee sysEmployee) {
        sysEmployeeMapper.update(sysEmployee);
    }

    public void delete(Integer id) {
        sysEmployeeMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            sysEmployeeMapper.delete(id);
        }
    }

    public SysEmployee login(SysEmployee sysEmployee) {
        String username = sysEmployee.getUsername();
        SysEmployee dbemployee = sysEmployeeMapper.selectbyusername(username);
        if(dbemployee == null) {
            throw new CustomException("500","账户不存在");
        }
        String password = sysEmployee.getPassword();
        if (!dbemployee.getPassword().equals(password)){
            throw new CustomException("500","账号或密码错误");
        }
        return dbemployee;
    }
}
